import { templateSubscribe } from '@/utils/api/message.js'
import { userLogin } from '@/utils/api/user'
export default {
	downloadImage: (url) => {
		uni.downloadFile({
			url: url,
			success: (res) => {
				uni.saveImageToPhotosAlbum({
					filePath: res.tempFilePath,
					success: () => {
						uni.showToast({
							title: '保存成功',
							icon: 'none',
							duration: 3000,
							mask: true
						});
					},
					fail: (res) => {
						if (res.errMsg == "saveImageToPhotosAlbum:fail auth deny") {
							uni.showToast({
								title: "打开右上角设置 开启打开相册权限",
								icon: 'none',
								duration: 5000,
							})
							return
						}
						if (res.errMsg == "saveImageToPhotosAlbum:fail cancel") {
							uni.showToast({
								title: "取消保存",
								icon: 'none',
							});
							return
						}
						uni.showToast({
							title: res.errMsg,
							icon: 'none',
						});
					},
				})
			},
			fail: (res) => {
				uni.showToast({
					title: res.errMsg,
					icon: 'none',
				});
			},
		})
	},
	downloadVideoFile(url) {
		uni.showLoading({
			mask: true,
			title: '视频处理中'
		})
		let filepath = wx.env.USER_DATA_PATH + '/' + new Date().valueOf() + '.mp4'
		uni.downloadFile({
			url: url,
			filePath: filepath,
			success: (res) => {
				if (res.statusCode === 200) {
					uni.hideLoading()
					uni.saveVideoToPhotosAlbum({
						filePath: res.filePath,
						success: function(res) {
							uni.showToast({
								title: "保存成功",
								icon: "none",
								duration: 3000,
							})
						},
						fail(res) {
							console.log("555", res)
							uni.showToast({
								title: res.errMsg,
								icon: "none",
								duration: 3000,
							})
						}
					})
				} else {
					console.log("2222", res)
					uni.hideLoading()
					uni.showToast({
						title: res.errMsg,
						icon: "none",
						duration: 3000,
					})
				}
			},
			fail(res) {
				console.log("3333", res)
				uni.hideLoading()
				uni.showToast({
					title: res.errMsg || "视频处理失败",
					icon: "none",
					duration: 3000,
				})
			}
		})
	},
	imagePreview: (imageUrl) => {
		wx.previewImage({
			urls: [imageUrl],
			showmenu: false,
			referrerPolicy: "origin"
		})
	},
	imagePreviewToSave: (imageUrl) => {
		wx.previewImage({
			urls: [imageUrl],
			showmenu: true,
			referrerPolicy: "origin"
		})
	},
	showToast: (title, time = 3000, mask = false) => {
		uni.showToast({
			title: title,
			duration: time,
			icon: "none",
			mask: mask,
		})
	},
	saveImageToPhotosAlbum(imageUrl) {
		wx.getImageInfo({
			src: imageUrl,
			success: res => {
				wx.saveImageToPhotosAlbum({
					filePath: res.path,
					success(res) {
						wx.showToast({
							title: '保存图片成功！',
						})
					},
					fail(err) {
						wx.showToast({
							title: '保存图片失败！',
						})
					}
				})
			},
			fail: err => {
				console.log(err, 'err')
			}
		})
	},
	randomIntFromInterval(min, max) {
		return new Promise((resolve, reject) => {
			let val = Math.floor(Math.random() * (max - min + 1) + min)
			console.log(val)
			resolve(val);
		})
	},
	templateSubscribe(template) {
		return new Promise((resolve, reject) => {
			uni.requestSubscribeMessage({
				tmplIds: [template],
				success(res) {
					if (res.errMsg == 'requestSubscribeMessage:ok' && res[template] == 'accept') {
						templateSubscribe({ // 请求服务端记录
							template_id: template
						}).then(res => {
							resolve(res)
							if (res.code == 1) {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
								return
							}
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						})
						return
					}
					uni.showToast({
						icon: 'none',
						title: '取消订阅',
					})
				},
				fail(res) {
					uni.showToast({
						title: '订阅失败',
						icon: 'none',
						duration: 3000
					})
				}
			})
		})
	},
	login: (channel) => {
		uni.navigateTo({
			url: '/pageA/login/login'
		})
	},
	shareImage: (url) => {
		wx.downloadFile({
			url: url,
			success: (res) => {
				wx.showShareImageMenu({
					path: res.tempFilePath,
					success() {
						uni.showToast({
							icon: 'none',
							title: '分享成功'
						})
					},
					fail(res) {
						console.log(res)
						if (res.errMsg == "showShareImageMenu:fail cancel") {
							uni.showToast({
								icon: 'none',
								title: "取消分享",
								duration: 3000,
							})
							return
						}
						uni.showToast({
							icon: 'none',
							title: '分享失败',
							duration: 3000,
						})
					}
				})
			}
		})
	}
}